<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-if="checkPermission(permissionRoles.oftengroup_config_upload)" :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
      <el-upload
        v-if="checkPermission(permissionRoles.oftengroup_config_download)"
        ref="upload"
        class="filter-item"
        :action="`api/gm/config/oftengroup/upload?pid=${pid}`"
        :headers="header"
        :on-success="successInfo"
        :on-error="errorInfo"
        :limit="1"
        :on-exceed="handleExceed"
        :file-list="fileList"
      >
        <el-button type="success" icon="el-icon-upload2">上传</el-button>
      </el-upload>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="730">
      <el-table-column label="配置ID" prop="cfg_id" align="center"></el-table-column>
      <el-table-column label="子类型" prop="type" align="center"></el-table-column>
      <el-table-column label="名称" prop="group_name" align="center"></el-table-column>
      <el-table-column label="等级" prop="cfg_value" align="center"></el-table-column>
    </el-table>
    <pagination v-show="total>0" :limit.sync="pageSize" :total="total" />
  </div>
</template>
<script>
import checkPermission from '@/utils/permission'
import Pagination from '@/components/Pagination'
import { getOftenGroupConfig } from '@/api/config'
export default {
  name: 'OftenGroup',
  components: { Pagination },
  data() {
    return {
      pid: 2, // 建筑类
      permissionRoles: this.$store.state.user.permissionRoles,
      header: {
        Authorization: this.$store.state.user.token
      },
      list: [],
      total: 0,
      pageSize: 1000,
      listLoading: false,
      downloadLoading: false,
      fileList: []
    }
  },
  created() {
    this.init()
  },
  methods: {
    checkPermission,
    init() {
      this.getList()
    },
    async getList() {
      this.listLoading = true
      const res = await getOftenGroupConfig()
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['配置ID', '子类型', '名称', '等级']
        const filterVal = ['cfg_id', 'type', 'group_name', 'cfg_value']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '道具配置'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    successInfo(res, file, fileList) {
      this.$message.success('上传成功')
      this.init()
    },
    errorInfo(res, file, fileList) {
      this.$message.error('上传失败，文档格式错误')
    }
  }
}
</script>
